package logic

import (
	"context"
	"fmt"
	"shop/model/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopInfoLogic {
	return &ShopInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopInfoLogic) ShopInfo(in *shoprpc.ShopInfoRequest) (*shoprpc.ShopInfoResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.Userid).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}

	var findshop mysql.Products
	mysql.DB.Where("id = ?", in.Id).Find(&findshop)
	if findshop.ID == 0 {
		return nil, fmt.Errorf("该商品不存在")
	}
	mysql.DB.First(&findshop)

	if in.Userid != findshop.Userid {
		return nil, fmt.Errorf("该商品不属于该用户")
	}

	return &shoprpc.ShopInfoResponse{
		Id:        int64(findshop.ID),
		Shopname:  findshop.Shopname,
		Shopimage: findshop.Shopimage,
		Price:     findshop.Price,
		Stock:     findshop.Stock,
		Userid:    findshop.Userid,
	}, nil
}
